<template>
  <div class="update-site">
    <ul>
      <li>
        <label
          ><span>姓名：</span><input type="text" v-model="addrdata.name"
        /></label>
      </li>
      <li>
        <label
          ><span>电话：</span><input type="text" v-model="addrdata.phone"
        /></label>
      </li>
      <li>
        <label
          ><span>地址：</span><textarea v-model="addrdata.address"></textarea>
        </label>
      </li>
      <li><button @click="updateAddr">保存</button></li>
    </ul>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { upAddresseeApi } from "../../../api";
export default {
  name: "UpdateSite",
  data() {
    return {
      key: true,
      addrdata: {
        name: "",
        phone: "",
        address: "",
      },
    };
  },
  computed: {
    ...mapState("user", ["addressee"]),
  },
  watch: {
    addressee: {
      immediate: true,
      deep: true,
      handler(newValue) {
        this.addrdata = newValue;
      },
    },
  },
  methods: {
    message(ms) {
      if (/.*name.*/.test(ms)) {
        return "姓名格式不正确！";
      }
      if (/.*phone.*/.test(ms)) {
        return "号码格式不正确！";
      }
      if (/.*address.*/.test(ms)) {
        return "地址格式不正确！";
      }
      return ms;
    },
    updateAddr() {
      if (this.key) {
        this.key = false;
      } else {
        return;
      }
      if (
        this.addrdata.name === "" &&
        this.addrdata.phone === "" &&
        this.addrdata.address === ""
      ) {
        this.key = true;
        this.$bus.$emit("showTips", "姓名，电话和地址不能为空！");
        return;
      } else if (this.addrdata.name === "" && this.addrdata.phone === "") {
        this.key = true;
        this.$bus.$emit("showTips", "姓名和电话不能为空！");
        return;
      } else if (this.addrdata.name === "" && this.addrdata.address === "") {
        this.key = true;
        this.$bus.$emit("showTips", "姓名和地址不能为空！");
        return;
      } else if (this.addrdata.phone === "" && this.addrdata.address === "") {
        this.key = true;
        this.$bus.$emit("showTips", "电话和地址不能为空！");
        return;
      } else if (this.addrdata.name === "") {
        this.key = true;
        this.$bus.$emit("showTips", "姓名不能为空！");
        return;
      } else if (this.addrdata.phone === "") {
        this.key = true;
        this.$bus.$emit("showTips", "电话不能为空！");
        return;
      } else if (this.addrdata.address === "") {
        this.key = true;
        this.$bus.$emit("showTips", "地址不能为空！");
        return;
      }
      if (!confirm("是否修改")) return (this.key = true);

      upAddresseeApi(
        (response) => {
          if (response.data.status === 1) {
            this.$bus.$emit("showTips", "收货地址修改成功！");
            this.key = true;
          } else {
            this.$bus.$emit("showTips", this.message(response.data.message));
            this.key = true;
          }
        },
        (err) => {
          this.$bus.$emit("showTips", "出错了！请检查网络是否连接！");
          this.key = true;
          console.log(err);
        },
        this.addrdata
      );
    },
  },
};
</script>

<style lang="stylus" scoped>
.update-site {
  width: 80%;
  color: #aaa;
  font-size: 14px;

  ul {
    padding-top: 20px;
    line-height: 50px;
  }

  label>span:first-child {
    display: inline-block;
    width: 90px;
    vertical-align: middle;
  }

  input {
    width: 180px;
    height: 22px;
    padding: 0 6px;
    border: 1px solid #999;
    vertical-align: middle;
  }

  textarea {
    width: 400px;
    height: 150px;
    padding: 6px;
    border: 1px solid #999;
    resize: none;
  }

  button {
    padding: 6px 20px;
    border: none;
    margin-left: 120px;
    border-radius: 4px;
    font-size: 14px;
    background-color: #fe4d1b;
    color: #fff;
  }
}
</style>